<?php
namespace app\common\service;

use app\common\logic\CartLogic;
use app\common\logic\GoodsLogic;
use think\Page;
use app\common\logic\wechat\WechatUtil;
use think\facade\Db;

class GetGoodDetail extends Base {
    static $AUTH_CHECK = false; //默认必须登录
    public function index(){
        $goods_id = input("id");

        $goodsLogic = new GoodsLogic();
        $listdata= ['map'=>['goods_id'=>['in',$goods_id]]];
        $goods = $goodsLogic->getListNew($listdata,true);
        if(empty($goods) || ($goods['is_on_sale'] == 0)){
            $result = [
                'status' => false,
                'msg' => '此商品不存在或者已下架',
                'data' => [],
            ];
            return $result;
        }
        $goods['goods_content'] = htmlspecialchars_decode($goods['goods_content']);
        $goods['leixing_name'] = Db::name('goods_leixing')->where('id',$goods['leixing_id'])->value('name');
        // 商品 图册
        $goods_images_list = explode(',',$goods['pics']);

        //查询默认地址
        $address = Db::name('user_address')->where(array( 'user_id' => $this->user_id))->order('is_default desc')->find();
        if($address){
            $area_arr = Db::name('region')->column('name','id');
            $address['province_name'] = $area_arr[$address['province']]??'';
            $address['city_name'] = $area_arr[$address['city']]??'';
            $address['district_name'] = $area_arr[$address['district']]??'';
            $address['town_name'] = $area_arr[$address['town']]??'';
        }
        $domain = getCurUrl();
        if(strpos($goods['original_img'],'http')===false){
            $goods['original_img'] = $domain.$goods['original_img'];
        }
        foreach($goods_images_list as $k=>$v){
            if(strpos($v,'http')===false){
                $goods_images_list[$k] = $domain.$v;
            }
        }
        $goods['pics'] = $goods_images_list;


        $business = [
            'name' => '',
            'logo' => ''
        ];
        $result = [
            'status' => true,
            'msg' => '',
            'data' => [
                'info' => $goods,
                'goods_images_list' => $goods_images_list,
                'address' => $address,
                'business' => $business
            ],
        ];
        return $result;
    }
    public function getguige(){
        $id = input('id');
        $product = Db::name('goods')->where('goods_id',$id)->find();
        $guige_data = json_decode($product['spec_items'],true);
        $ggselected = [];
        if($guige_data){
            foreach($guige_data as $v) {
                $ggselected[] = 0;
            }
        }

        $guige_lists = Db::name('spec_goods_price')->where('goods_id',$id)->column('*','key');
        foreach($guige_lists as $k=>$v){
            $res_seckill = logic('GoodsLogic')->getSeckillPrice($id,$v['item_id']);
            if($res_seckill['is_seckill']==1){
                $guige_lists[$k]['shop_price'] = $res_seckill['shop_price'];
            }
        }

        $result = [
            'status' => true,
            'msg' => '',
            'data' => [
                'product' => $product,
                'guige_lists' => $guige_lists,
                'guige_data' => $guige_data,
                'ggselected' => $ggselected,
            ],
        ];
        return $result;
    }

}
